package com.xique.door.project.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.door.bean.domain.ParkInfo;
import com.xique.door.bean.domain.Repair;
import com.xique.door.bean.domain.User;
import com.xique.door.mapper.RepairMapper;
import com.xique.door.mapper.UserMapper;
import com.xique.door.project.miniprogram.bean.vo.MiniRepairVO;
import com.xique.door.project.miniprogram.bean.vo.ParkInfoVO;
import com.xique.door.project.miniprogram.service.IMiniRepairService;
import com.xique.door.service.IBaseUserService;
import com.xique.door.service.IParkInfoService;
import com.xique.door.service.IUserService;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MiniRepairServiceImpl extends ServiceImpl<RepairMapper, Repair>
        implements IMiniRepairService {

    @Autowired
    private IBaseUserService baseUserService;

    @Autowired
    private IUserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IParkInfoService parkInfoService;

    @Override
    public AjaxResult insertMiniRepair(Repair repair) {
        User userSearch = new User();
        userSearch.setBaseUserId(SecurityUtils.getBaseUserId());
        userSearch.setParkId(repair.getParkId());
        List<User> userList = userMapper.selectUsers(userSearch);
        if (userList != null || userList.size() > 0) {
            repair.setCreateTime(new Date());
            repair.setUpdateTime(new Date());
            repair.setUserId(userList.get(0).getId());
            repair.setBaseUserId(SecurityUtils.getBaseUserId());
            int rest = baseMapper.insert(repair);
            if (rest > 0) {
                return AjaxResult.success("添加成功");
            }
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("该用户没有小区的权限");
    }

    @Override
    public AjaxResult getMiniRepair(Long id, Long userId) {
        QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        Repair repair = baseMapper.selectOne(queryWrapper);
        String str = repair.getImages();
        String[] image = str.split(",");
        MiniRepairVO vo = new MiniRepairVO();
        vo.setName(repair.getName());
        vo.setAddress(repair.getAddress());
        if (repair.getStatus().equals("1")) {
            vo.setStatus("处理中");
        } else {
            vo.setStatus("已处理");
        }
        vo.setCreateTime(repair.getCreateTime());
        vo.setRepairType(repair.getRepairType());
        vo.setPhone(repair.getPhone());
        vo.setParkId(repair.getParkId());
        vo.setContent(repair.getContent());
        vo.setImage(image);
        vo.setId(repair.getId());
        return AjaxResult.success(vo);
    }

    @Override
    public PageResult getRepairList(
            String status, Integer pageNum, Integer pageSize, Long parkId, Long userId) {

        QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
        if(parkId!=null&&!"".equals(parkId)){
            queryWrapper.eq("park_id", parkId);
        }

       // queryWrapper.eq("base_user_id", SecurityUtils.getBaseUserId());

        queryWrapper.orderByDesc("create_time");
        if (status != null && !"".equals(status)) {
            queryWrapper.eq("status", status);
        }
        Page<Repair> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<Repair> list = baseMapper.selectPage(page, queryWrapper);
        List<Repair> list1 = list.getRecords();
        List<MiniRepairVO> voList = new ArrayList<>();
        if (list1.size() > 0) {
            for (Repair re : list1) {
                MiniRepairVO vo = new MiniRepairVO();
                vo.setId(re.getId());
                vo.setName(re.getName());
                vo.setAddress(re.getAddress());
                vo.setStatus(re.getStatus());
                vo.setCreateTime(re.getCreateTime());
                vo.setRepairType(re.getRepairType());
                vo.setImage(re.getImage());
                voList.add(vo);
            }
        }
        long totalPages = list.getPages();
        long totalSize = list.getTotal();
        return PageResult.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .totalPages(totalPages)
                .totalSize(totalSize)
                .list(voList)
                .build();
    }

    public AjaxResult seleceteParkInfo() {
        User userSearch = new User();
        userSearch.setBaseUserId(SecurityUtils.getBaseUserId());
        List<User> userList = userMapper.selectUsers(userSearch);
        if (userList != null || userList.size() > 0) {
            List<Long> parkId = new ArrayList<>();
            userList.forEach(item -> {
                parkId.add(item.getParkId());
            });
            parkId.stream()
                    .distinct()
                    .collect(Collectors.toList());
            List<ParkInfo> parkInfos= parkInfoService.selectParkInfoByIds(parkId);

            List<ParkInfoVO> voList=new ArrayList<>();
            parkInfos.forEach(item -> {
                ParkInfoVO vo=new ParkInfoVO();
                vo.setId(item.getId());
                vo.setName(item.getName());
                voList.add(vo);
            });
            return AjaxResult.success(voList);
        }
        return AjaxResult.success("该用户没有绑定房屋");
    }

}
